package com.travelbid.admin.struts.action;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.struts.ActionSupport;

import com.travelbid.admin.pojo.TUser;
import com.travelbid.admin.service.AdminUserService;


public class UpdatePasswordAction extends ActionSupport{
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setContentType("text;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();  
		AdminUserService service = (AdminUserService)getWebApplicationContext().getBean("AdminUserService");
		try {
			String oldPassword=request.getParameter("oldPassword");
			String newPassword=request.getParameter("newPassword");
			TUser user=(TUser)request.getSession().getAttribute("user");
			if(user.getPwd().equals(oldPassword)){
				user.setPwd(newPassword);
				service.AddNewUser(user);
				request.getSession().setAttribute("user", user);
				out.print("success");
			}else{
				out.print("fail");
			}
		}
		catch (RuntimeException  e) {
			e.printStackTrace();
			out.print("fail");
		}
		return null;
	}
}
